﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Enums;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;

namespace Rio.Framework.Pages.Controls.BaseClass
{


    public class ListControlBase : ControlBase, INotifyPropertyChanged
    {

        #region Attributs

        protected QueryableCollectionView m_oSource;
        protected SplashScreen m_ssSplashScreen;

        protected FlowEnums.ChronoTypeEnum m_oFolderTypeFlow = FlowEnums.ChronoTypeEnum.None;
        protected GridViewRow m_gvrLastExpandedRow;

        protected string labelColOriginalNameFile;
        protected string labelColAuthor;
        protected string labelColNote;
        protected string labelColPhotoCredit;
        protected string labelColPublicationDate;
        protected string labelNbrOfDocumentResults;
        protected string LabelHeaderColumnNbDoc;
        protected string labelMessageDeleteConfirmation;
        protected string labelNbrOfFolderResults;
        protected string labelAlerts;
        protected string LabelHeaderColumnCreationDate;
        protected string LabelHeaderColumnModificationDate;
        #endregion

        #region Properties

        /// <summary>
        /// Current selected Organization
        /// </summary>
        public NetBay.Core.BusinessEntities.Organization OrganizationSelected { get; set; }

        /// <summary>
        /// Current selected FolderType
        /// </summary>
        public NetBay.Core.BusinessEntities.FolderType FolderTypeSelected { get; set; }

        /// <summary>
        /// List of index to display in the grid
        /// </summary>
        public virtual List<NetBay.Core.BusinessEntities.Index> ListIndex { get; set; }

        /// <summary>
        /// Indicate whether the past action is enabled
        /// Only if the user can scan in the TD and have documents in the clipboard
        /// </summary>
        public bool isPastEnabled
        {
            get
            {
                return (GlobalWebContext.GetUserCanScann(FolderTypeSelected)) ? GlobalWebContext.CopyPasteManager.HasDocument : false;
            }
        }

        public static DateTime ToDateTime(string p_oSource)
        {
            DateTime l_oDt;
            return DateTime.TryParse(p_oSource, out l_oDt) ? l_oDt : default(DateTime);
        }
        #endregion

        #region Protected Functions

        /// <summary>
        /// Get the type of an index
        /// </summary>
        /// <param name="p_oIndex"></param>
        /// <returns></returns>
        protected Type GetIndexType(NetBay.Core.BusinessEntities.Index p_oIndex)
        {
            Type l_oType;
            switch (p_oIndex.Type)
            {
                case (int)IndexDomainServiceEnums.IndexType.Date:
                    l_oType = typeof(DateTime?);
                    break;
                case (int)IndexDomainServiceEnums.IndexType.Integer:
                    l_oType = typeof(string); // string because filtering no run with integer and string value
                    break;
                case (int)IndexDomainServiceEnums.IndexType.Double:
                    l_oType = typeof(Double);
                    break;
                case (int)IndexDomainServiceEnums.IndexType.Boolean:
                    l_oType = typeof(bool);
                    break;
                default:
                    l_oType = typeof(string);
                    break;
            }
            return l_oType;
        }

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Called when [notify property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event EventHandler RefreshSearchResult;
        protected void OnRefreshSearchResult()
        {
            if (this.RefreshSearchResult != null)
            {
                RefreshSearchResult(this, new EventArgs());
            }
        }

        public event DocumentPastedHandler DocumentPasted;
        private void OnDocumentPasted(NetBay.Core.BusinessEntities.Folder Folder)
        {
            if (DocumentPasted != null)
                DocumentPasted(this, Folder);
        }

        #endregion

        #region Paste

        /// <summary>
        /// Past documents in the selected folder
        /// </summary>
        /// <param name="Folder">Selected folder</param>
        protected void PasteDocuments(NetBay.Core.BusinessEntities.Folder Folder)
        {
            if (Folder != null)
            {
                if (GlobalWebContext.CopyPasteManager.ActionType == PasteCopyActionType.Cut)
                {
                    //Call Cut-Past service
                    GetDataContext.PasteDocument(GlobalWebContext.CopyPasteManager.Documents, Folder.Iddoc, Folder.Identifier, FolderTypeSelected.Identifier, true, null,
                        GlobalWebContext.CopyPasteManager.VersionIdentifier, GlobalWebContext.CopyPasteManager.IsOriginalVersion, PasteDocumentsQueryCallback, null);
                }
                else if (GlobalWebContext.CopyPasteManager.ActionType == PasteCopyActionType.Copy)
                {
                    //Call Copy-Past service
                    GetDataContext.PasteDocument(GlobalWebContext.CopyPasteManager.Documents, Folder.Iddoc, Folder.Identifier, FolderTypeSelected.Identifier, false, null,
                        GlobalWebContext.CopyPasteManager.VersionIdentifier, GlobalWebContext.CopyPasteManager.IsOriginalVersion, PasteDocumentsQueryCallback, null);
                }
            }
        }

        /// <summary>
        /// PastDocuments Callback
        /// </summary>
        /// <param name="e"></param>
        private void PasteDocumentsQueryCallback(InvokeOperation<ReturnBoolean> e)
        {
            if (!e.HasError && e.Value != null && e.Value.Value)
            {
                //raise event to the searchpage 
                var sourceFolder = GlobalWebContext.CopyPasteManager.SourceFolder;
                GlobalWebContext.CopyPasteManager.ClearDocuments();
                OnDocumentPasted(sourceFolder);
                OnPropertyChanged("isPastEnabled");
                OnRefreshSearchResult();
            }
        }

        #endregion

        public virtual void ClearResults() { }
    }
}
